/**
 * JAVACC DEMO 1.0
 * @copy right apache company All rights reserved. 
 * @Package com.apache.method  
 */
package com.apache.license.filter;

import javax.servlet.Filter;
import javax.servlet.ServletException;

import org.apache.log4j.Logger;

import com.apache.license.validator.LicenseValidate;

/**
 * description:  启用license父类filter
 * @author Hou Dayu 创建时间：2015-12-28  
 * @Copyright(c)2014:
 */
public abstract class AbstractFilter implements Filter {

	private boolean isUseLicense = false;
	private Logger log = Logger.getLogger(getClass());

	//license文件初始化
	protected void initLincense() throws ServletException {
		String path = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
		path = path.substring(1);
		String publicpath = path + "/config/dwusoft_public.dll";
		String privatepath = path + "/config/dwusoft.dll";
		try {
			initLicense("", publicpath, privatepath);
		} catch (Exception e) {
			try {
				initLicense("jar", "/config/dwusoft_public.dll", "/config/dwusoft.dll");
			} catch (Exception e1) {
				log.debug("产品还没有被授权或License 超过有效期！");
				System.exit(0);
			}
		}
	}

	private void initLicense(String path, String publicpath, String privatepath) throws Exception {
		if (!isUseLicense) {
			if ("".equals(path))
				isUseLicense = LicenseValidate.getInstance().validate(publicpath, privatepath);
			else
				isUseLicense = LicenseValidate.getInstance().validate(path, publicpath, privatepath);
			if (!isUseLicense) {
				log.debug("无效的产品License！");
				System.exit(0);
			} else {
				if (LicenseValidate.getInstance().islasttime()) {
					log.debug("你的License起始有效日期为：" + LicenseValidate.getInstance().getMsg().getBegintime().toString());
					log.debug("有效期为：" + LicenseValidate.getInstance().getMsg().getTimeleng() + "个月");
					log.debug("授权公司：" + LicenseValidate.getInstance().getMsg().getCompany());
				} else {
					log.debug("License 超过有效期！");
					System.exit(0);
				}
			}

		}
	}

}
